// Test script to verify the import fix
const fs = require('fs');

// Read the test JSON files
const testfull = JSON.parse(fs.readFileSync('src/testJson/Testfull.json', 'utf8'));
const testpart = JSON.parse(fs.readFileSync('src/testJson/testpart.json', 'utf8'));

// Simulate the validation logic from the fixed code
function validateFoodList(importData) {
  if (!importData.foodList || !Array.isArray(importData.foodList)) {
    return { valid: false, reason: '文件格式不正确' };
  }

  const isValidFoodList = importData.foodList.every((item) => {
    return item.id && 
           item.name && 
           item.category && 
           typeof item.quantity === 'number' && 
           item.hasOwnProperty('unit') && 
           item.purchaseDate && 
           item.hasOwnProperty('expiryDate');
  });

  if (!isValidFoodList) {
    return { valid: false, reason: '食材数据格式不正确' };
  }

  return { valid: true, reason: '验证通过' };
}

// Simulate the calculateStatus function
function calculateStatus(expiryDate) {
  if (!expiryDate || expiryDate.trim() === '') {
    return 'normal';
  }

  const today = new Date();
  today.setHours(0, 0, 0, 0);
  const expiry = new Date(expiryDate);
  
  if (isNaN(expiry.getTime())) {
    return 'normal';
  }
  
  expiry.setHours(0, 0, 0, 0);
  const diffTime = expiry.getTime() - today.getTime();
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

  if (diffDays < 0) {
    return 'expiring';
  } else if (diffDays <= 3) {
    return 'expiring';
  } else if (diffDays <= 7) {
    return 'low';
  } else {
    return 'normal';
  }
}

console.log('=== 测试导入修复 ===\n');

// Test Testfull.json
console.log('测试 Testfull.json:');
const testfullResult = validateFoodList(testfull);
console.log(`验证结果: ${testfullResult.valid ? '✅ 通过' : '❌ 失败'} - ${testfullResult.reason}`);
if (testfullResult.valid) {
  console.log('食材状态计算:');
  testfull.foodList.forEach(item => {
    const status = calculateStatus(item.expiryDate);
    console.log(`  - ${item.name}: ${status} (过期日期: ${item.expiryDate || '无'})`);
  });
}

console.log('\n测试 testpart.json:');
const testpartResult = validateFoodList(testpart);
console.log(`验证结果: ${testpartResult.valid ? '✅ 通过' : '❌ 失败'} - ${testpartResult.reason}`);
if (testpartResult.valid) {
  console.log('食材状态计算:');
  testpart.foodList.forEach(item => {
    const status = calculateStatus(item.expiryDate);
    console.log(`  - ${item.name}: ${status} (过期日期: ${item.expiryDate || '无'})`);
  });
}

console.log('\n=== 修复总结 ===');
console.log('1. 修改了 calculateStatus 函数，支持空的过期日期');
console.log('2. 修改了验证逻辑，使用 hasOwnProperty 检查字段存在性而不是值的真假性');
console.log('3. 现在可以导入包含空字符串字段的部分数据');
